<?php

class sbData {

    public static function addCountry($en, $he, $ru) {
        $country = tblCountryTable::getInstance()->findByDql('name_en=?', $en)->getFirst();
        if (!$country) {
            $country = new tblCountry();
            $country->setNameEn($en);
            $country->setNameRu($he);
            $country->setNameHe($ru);
            $country->setCreateTime(sbValues::getCurrentTime());
            $country->save();
        }
    }

    public static function editCountry($en, $he, $ru, $id) {
        $country = tblCountryTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($country) {
            $country->setNameEn($en);
            $country->setNameRu($he);
            $country->setNameHe($ru);
            $country->setCreateTime(sbValues::getCurrentTime());
            $country->save();
        }
    }

    public static function addRegion($en, $he, $ru, $country) {
        $region = new tblRegion();
        $region->setNameEn($en);
        $region->setNameHe($he);
        $region->setNameRu($ru);
        $region->setCountryId($country);
        $region->setCreateTime(sbValues::getCurrentTime());
        $region->save();
    }

    public static function editRegion($en, $he, $ru, $country, $id) {
        $region = tblRegionTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($region) {
            $region->setNameEn($en);
            $region->setNameHe($he);
            $region->setNameRu($ru);
            $region->setCountryId($country);
            $region->setCreateTime(sbValues::getCurrentTime());
            $region->save();
        }
    }

    public static function addCity($en, $he, $ru, $country, $region, $priority) {
        $city = tblCityTable::getInstance()->findByDql('name_en=? and country_id=?', array($en, $country))->getFirst();
        if (!$city) {
            $city = new tblCity();
            $city->setNameEn($en);
            $city->setNameHe($he);
            $city->setNameRu($ru);
            $city->setCountryId($country);
            $city->setRegionId($region);
            $city->setPriority($priority);
            $city->setCreateTime(sbValues::getCurrentTime());
            $city->save();
        }
    }

    public static function editCity($en, $he, $ru, $country, $region, $priority, $id) {
        $city = tblCityTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($city) {
            $city->setNameEn($en);
            $city->setNameHe($he);
            $city->setNameRu($ru);
            $city->setCountryId($country);
            $city->setRegionId($region);
            $city->setPriority($priority);
            $city->setCreateTime(sbValues::getCurrentTime());
            $city->save();
        }
    }

    public static function addProvider($name, $short, $username, $pass) {
        $provider = new tblProvider();
        $provider->setName($name);
        $provider->setShortName($short);
        $provider->setUsername($username);
        $provider->setPass($pass);
        $provider->setUpdateTime(sbValues::getCurrentTime());
        $provider->setCreateTime(sbValues::getCurrentTime());
        $provider->save();
    }

    public static function editProvider($name, $short, $username, $pass, $id) {
        $provider = tblProviderTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($provider) {
            $provider->setName($name);
            $provider->setShortName($short);
            $provider->setUsername($username);
            $provider->setPass($pass);
            $provider->setUpdateTime(sbValues::getCurrentTime());
            $provider->save();
        }
    }

    public static function addUser($title, $first_name, $last_name, $email, $password, $phone1, $phone2, $fax, $country, $city, $address, $permissions, $agent, $status, $commissions,$latitude,$logitude) {
        $user = tblUsersTable::getInstance()->findByDql('email=? and status=?', array($email, sbValues::STATUS_ACTIVE))->getFirst();
        
        if ($user) {
            return 100;
        } else {
           
          
            if ($permissions == sbValues::TYPE_USER_STANDART) {
                $per = sbValues::PERMISSIONS_STANDART;
                $agent = sbValues::PERMISSIONS_STANDART;
            } else if ($permissions == sbValues::TYPE_USER_AGENT) {
                $per = sbValues::PERMISSIONS_AGENT;
            } else if ($permissions == sbValues::TYPE_USER_FIRM) {
                $per = sbValues::PERMISSIONS_FIRM;
                $agent = sbValues::PERMISSIONS_FIRM;
            } else if ($permissions == sbValues::TYPE_USER_ADMIN) {
                $per = sbValues::PERMISSIONS_ADMIN;
            }
            
            $agent = new sbAgent();
            $latlong = $agent->findMapPosition($country, $city, $address);
 
            $user = new tblUsers();
            $user->setTitle($title);
            $user->setFirstName($first_name);
            $user->setLastName($last_name);
            $user->setEmail($email);
            $user->setPassword(sha1($password));
            $user->setPhone1($phone1);
            $user->setPhone2($phone2);
            $user->setFax($fax);
            $user->setStatus($status);
            $user->setCountry($country);
            $user->setCity($city);
            $user->setAddress($address);
            $user->setPermissions($per);
            $user->setCommissions($commissions);
            $user->setCredit(0);
            $user->setBalance(0);
            $user->setReputation(0);
            $user->setCommissions(0);
            $user->setOrders(0);
             $user->setLatitude($latitude);
             $user->setLogitude($logitude);
           /* if ($latlong) {
                $user->setLatitude($latlong['latitude']);
                $user->setLogitude($latlong['longitude']);
            }*/
            $user->setCreateTime(sbValues::getCurrentTime());
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->save();
           
            //echo $agent->getId()."<hr>";
            $structure = new tblUsersStructure();
            $structure->setSubsidiaryId($user->getId());
            //must be agentid
             #$structure->setMainId(3);
            $structure->setMainId($agent);
            $structure->setStatus($status);
            $structure->setUpdateTime(sbValues::getCurrentTime());
            $structure->setCreateTime(sbValues::getCurrentTime());
            $structure->save();

            return 1;
        }
    }

    public static function editUser($id, $title, $first_name, $last_name, $email, $password, $phone1, $phone2, $fax, $country, $city, $address, $permissions, $agent, $status, $commissions,$latitude,$logitude) {
        $user = tblUsersTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if (!$user) {
            return 200;
        } else {
            if ($permissions == sbValues::TYPE_USER_STANDART) {
                $per = sbValues::PERMISSIONS_STANDART;
                $agent = sbValues::PERMISSIONS_STANDART;
            } else if ($permissions == sbValues::TYPE_USER_AGENT) {
                $per = sbValues::PERMISSIONS_AGENT;
            } else if ($permissions == sbValues::TYPE_USER_FIRM) {
                $per = sbValues::PERMISSIONS_FIRM;
                $agent = sbValues::PERMISSIONS_FIRM;
            } else if ($permissions == sbValues::TYPE_USER_ADMIN) {
                $per = sbValues::PERMISSIONS_ADMIN;
            }
            
            $agent = new sbAgent();
           /* $latlong = $agent->findMapPosition($country, $city, $address);
            
            if ($latlong) {
                $user->setLatitude($latlong['latitude']);
                $user->setLogitude($latlong['longitude']);
            }*/

            $user->setTitle($title);
            $user->setFirstName($first_name);
            $user->setLastName($last_name);
            $user->setEmail($email);
            $user->setPassword(sha1($password));
            $user->setPhone1($phone1);
            $user->setPhone2($phone2);
            $user->setFax($fax);
            $user->setStatus($status);
            $user->setCountry($country);
            $user->setCity($city);
            $user->setAddress($address);
            $user->setPermissions($per);
            $user->setCommissions($commissions);
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->setLatitude($latitude);
            $user->setLogitude($logitude);
            $user->save();

            $structure = tblUsersStructureTable::getInstance()->findByDql('subsidiary_id=?', $id)->getFirst();
            $structure->setMainId($agent);
            $structure->setStatus($status);
            $structure->setUpdateTime(sbValues::getCurrentTime());
            $structure->save();

            return 1;
        }
    }

    public static function deactivateUser($id) {
        $user = tblUsersTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($user) {
            if ($user->getStatus() == sbValues::STATUS_ACTIVE) {
                $user->setStatus(sbValues::STATUS_DEACTIVE);
            } else {
                $user->setStatus(sbValues::STATUS_ACTIVE);
            }
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->save();
        }
    }

    public static function addHotel($name_en, $name_ru, $name_he, $stars, $country, $region, $city, $address, $phone1, $phone2, $fax, $email, $description_en, $description_ru, $description_he, $priority, $images, $provider, $web) {
        $hotel = tblHotelTable::getInstance()->findByDql('name_en=? and country_id=? and city_id=? and provider_id=?', array($name_en, $country, $city, $provider))->getFirst();
        if (!$hotel) {
            $hotel = new tblHotel();
            $hotel->setNameEn($name_en);
            $hotel->setNameRu($name_ru);
            $hotel->setNameHe($name_he);
            $hotel->setProviderId($provider);
            $hotel->setCountryId($country);
            $hotel->setRegionId($region);
            $hotel->setCityId($city);
            $hotel->setStars($stars);
            $hotel->setAddress($address);
            $hotel->setPhone1($phone1);
            $hotel->setPhone2($phone2);
            $hotel->setFax($fax);
            $hotel->setEmail($email);
            $hotel->setWebSite($web);
            $hotel->setDescriptionEn($description_en);
            $hotel->setDescriptionRu($description_ru);
            $hotel->setDescriptionHe($description_he);
            $hotel->setPriority($priority);
            $hotel->setUpdateTime(sbValues::getCurrentTime());
            $hotel->setCreateTime(sbValues::getCurrentTime());
            $hotel->save();

            if ($images) {
                $count = count($images);
                $string = "";
                for ($i = 0; $i < $count; $i++) {
                    $image = explode(',', $images[$i]);
                    try {
                        $binary = base64_decode($image[1]);
                        $path = sfConfig::get('sf_upload_dir') . '/' . $hotel->getId() . '_' . $i . '.jpg';
                        $success = file_put_contents($path, $binary);

                        $url = sbValues::URL_LOCALHOST . $hotel->getId() . '_' . $i . '.jpg';

                        $string = $string . $url . ';';
                    } catch (Exception $e) {
                        $string = $e;
                    }
                }

                $hotel->setPhoto($string);
                $hotel->save();
            }

            return 1;
        } else {
            return 100;
        }
    }

    public static function editHotel($name_en, $name_ru, $name_he, $stars, $country, $region, $city, $address, $phone1, $phone2, $fax, $email, $description_en, $description_ru, $description_he, $priority, $images, $provider, $web, $id) {
        $hotel = tblHotelTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($hotel) {
            $hotel->setNameEn($name_en);
            $hotel->setNameRu($name_ru);
            $hotel->setNameHe($name_he);
            $hotel->setProviderId($provider);
            $hotel->setCountryId($country);
            $hotel->setRegionId($region);
            $hotel->setCityId($city);
            $hotel->setStars($stars);
            $hotel->setAddress($address);
            $hotel->setPhone1($phone1);
            $hotel->setPhone2($phone2);
            $hotel->setFax($fax);
            $hotel->setEmail($email);
            $hotel->setWebSite($web);
            $hotel->setDescriptionEn($description_en);
            $hotel->setDescriptionRu($description_ru);
            $hotel->setDescriptionHe($description_he);
            $hotel->setPriority($priority);
            $hotel->setUpdateTime(sbValues::getCurrentTime());
            $hotel->save();

            if ($images) {
                $count = count($images);
                $string = "";
                for ($i = 0; $i < $count; $i++) {
                    $image = explode(',', $images[$i]);
                    if (count($image) >= 2) {
                        try {
                            $binary = base64_decode($image[1]);
                            $path = sfConfig::get('sf_upload_dir') . '/' . $hotel->getId() . '_' . $i . '.jpg';
                            $success = file_put_contents($path, $binary);

                            $url = sbValues::URL_LOCALHOST . $hotel->getId() . '_' . $i . '.jpg';

                            $string = $string . $url . ';';
                        } catch (Exception $e) {
                            $string = $e;
                        }
                    } else {
                        $string = $string . $image[0] . ';';
                    }
                }

                $hotel->setPhoto($string);
                $hotel->save();
            } else {
                $hotel->setPhoto(NULL);
                $hotel->save();
            }

            return 1;
        } else {
            return 100;
        }
    }

    public static function deleteHotel($id) {
        Doctrine::getTable('tblHotel')
                ->createQuery()
                ->delete()
                ->where('id = ?', $id)
                ->execute();
    }

    public static function addPlan($en, $ru, $he) {
        $plan = tblPlansTable::getInstance()->findByDql('name_en=?', $en)->getFirst();
        if (!$plan) {
            $plan = new tblPlans();
            $plan->setNameEn($en);
            $plan->setNameRu($ru);
            $plan->setNameHe($he);
            $plan->setCreateTime(sbValues::getCurrentTime());
            $plan->setUpdateTime(sbValues::getCurrentTime());
            $plan->save();
        }
    }

    public static function editPlan($en, $ru, $he, $id) {
        $plan = tblPlansTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($plan) {
            $plan->setNameEn($en);
            $plan->setNameRu($ru);
            $plan->setNameHe($he);
            $plan->setUpdateTime(sbValues::getCurrentTime());
            $plan->save();
        }
    }

    public static function addRoom($hotel_id, $name, $description_en, $description_ru, $description_he, $adult, $children, $code, $baby) {
        $room = new tblRooms();
        $room->setHotelId($hotel_id);
        $room->setName($name);
        $room->setCodeName($code);
        $room->setDescriptionEn($description_en);
        $room->setDescriptionRu($description_ru);
        $room->setDescriptionHe($description_he);
        $room->setAdult($adult);
        $room->setChildren($children);
        $room->setBaby($baby);
        $room->setStatus(sbValues::STATUS_ACTIVE);
        $room->setUpdateTime(sbValues::getCurrentTime());
        $room->setCreateTime(sbValues::getCurrentTime());
        $room->save();
    }

    public static function editRoom($hotel_id, $name, $description_en, $description_ru, $description_he, $adult, $children, $id, $code, $baby) {
        $room = tblRoomsTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($room) {
            $room->setName($name);
            $room->setCodeName($code);
            $room->setDescriptionEn($description_en);
            $room->setDescriptionRu($description_ru);
            $room->setDescriptionHe($description_he);
            $room->setAdult($adult);
            $room->setChildren($children);
            $room->setBaby($baby);
            $room->setUpdateTime(sbValues::getCurrentTime());
            $room->save();
        }
    }

    public static function addPrice($hotel_id, $room_id, $type, $date, $period, $fit, $cascade, $special, $commission, $market) {
        $room = new tblPrice();
        $room->setHotelId($hotel_id);
        $room->setRoomId($room_id);
        $room->setType($type);
        $room->setDate($date);
        $room->setPeriod($period);
        $room->setFitPriceUsd($fit);
        $room->setCascadePriceUsd($cascade);
        $room->setSpecialPriceUsd($special);
        $room->setCommission($commission);
        $room->setRussianMarketUsd($market);
        $room->setUpdateTime(sbValues::getCurrentTime());
        $room->setCreateTime(sbValues::getCurrentTime());
        $room->save();
    }

    public static function editPrice($hotel_id, $room_id, $type, $date, $period, $fit, $cascade, $special, $id, $commission, $market) {

        $room = tblPriceTable::getInstance()->findByDql('id=?', $id)->getFirst();
        if ($room) {
            $room->setType($type);
            $room->setDate($date);
            $room->setPeriod($period);
            $room->setFitPrice($fit);
            $room->setCascadePrice($cascade);
            $room->setSpecialPrice($special);
            $room->setCommission($commission);
            $room->setRussianMarket($market);
            $room->setUpdateTime(sbValues::getCurrentTime());
            $room->save();
        }
    }

}
